package com.sduept.nwld.wave.manager;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.nwld.dataserver.enums.CfgValidateEnum;
import com.sduept.nwld.dataserver.util.CimUtils;
import com.sduept.nwld.wave.util.CfgChnsParserUtil;
import com.sduept.stardfile.parser.model.cfg.CfgAnalogChn;
import com.sduept.stardfile.parser.model.cfg.CfgColumnNumException;
import com.sduept.stardfile.parser.model.cfg.CfgFile;
import com.sduept.stardfile.parser.model.cfg.CfgTimeFormatException;
import com.sduept.stardfile.parser.model.cfg.CfgTimeInfo;
import com.sduept.utils.DateUtil;

/**
 * @author     Xiaopy
 * @version    V1.0
 * @Date       2018年12月13日 下午4:16:16
 */
public class CfgFileValidateUtil {
	
	private static final String PARAM_EMPTY="";
	
	/**
	 * 根据cfg 文件内容判断是否包含本次故障相关信息
	 * 注：只返回异常信息，保护录波无法检测故不做校验
	 * @param cfgContent 录波文件内容
	 * @param record 故障记录
	 * @param isCheckEquipment 是否检查录波文件
	 * @return
	 */
	public static String getValidateCfgMsg(String cfgContent,FaultRecord record,boolean isCheckEquipment){
		String message="";
		try {
			CfgFile cfgFile = new CfgFile();
			cfgFile.ReadCfgFile(cfgContent);
			ValidateContent faultTimeContent = validateFaultTime(cfgFile,record);
			if(CfgValidateEnum.SUCCESS!=faultTimeContent.getMsgType()){
				if(CfgValidateEnum.EXCEPTION==faultTimeContent.getMsgType()){
					message=message+faultTimeContent.getMsgType().getValue()+" <br/>";
				}else{
					message=message+faultTimeContent.getMsgType().getValue()+","+faultTimeContent.getMsg()+" <br/>";
				}
			}
			if(!isCheckEquipment){
				return message;
			}
			ValidateContent equipmentContent = validateFaultEquipment(cfgFile,record);
			if(CfgValidateEnum.SUCCESS!=equipmentContent.getMsgType()){
				if(CfgValidateEnum.EXCEPTION==faultTimeContent.getMsgType()){
					message=message+equipmentContent.getMsgType().getValue()+" <br/>";
				}else{
					message=message+equipmentContent.getMsgType().getValue()+","+equipmentContent.getMsg()+" <br/>";
				}
				
			}
		} catch (NumberFormatException | IndexOutOfBoundsException | CfgColumnNumException | CfgTimeFormatException e) {
			e.printStackTrace();
			message=CfgValidateEnum.EXCEPTION.getValue();
		}
		return message;
	}
	
	/**
	 * 校验是否包含本次故障的设备
	 * @param cfgFile
	 * @param record
	 * @return
	 */
	private static ValidateContent validateFaultEquipment(CfgFile cfgFile,FaultRecord record){
		CfgAnalogChn[] ans=cfgFile.analogChns;
		String msgConten="本次故障的设备："+record.getEquipment();
		if(0==cfgFile.analogChns.length){
			return new ValidateContent(msgConten,CfgValidateEnum.FAILED_EQUIPMENT);
		}
		boolean valiadteEquipment=true;
		if("线路".equals(record.getEtype())){
			valiadteEquipment=lineValidate(ans,record.getEquipment());
		}else if("母线".equals(record.getEtype())){
			valiadteEquipment=busValidate(ans,record.getEquipment());
		}else if("变压器".equals(record.getEtype())){
			valiadteEquipment=powerValidate(ans,record.getEquipment());
		}
		if(valiadteEquipment){
			return new ValidateContent(PARAM_EMPTY,CfgValidateEnum.SUCCESS);
		}
		return new ValidateContent(msgConten,CfgValidateEnum.FAILED_EQUIPMENT);
	}
	
	/**
	 * 校验录波文件的时间和故障时间
	 * @param cfgFile
	 * @param record
	 * @return
	 */
	private static ValidateContent validateFaultTime(CfgFile cfgFile,FaultRecord record){
		String[] time = parseWaveTime(cfgFile);
		String startTime=time[0];
		if(StringUtils.isEmpty(startTime)){
			ValidateContent content = new ValidateContent(PARAM_EMPTY,CfgValidateEnum.EXCEPTION);
			return content;
		}
		Date date = DateUtil.getDateByString(startTime, DateUtil.pattern_ymdhms);
		long time1 = date.getTime();
		long time2 = record.getFaultTime().getTime();
		long abs = Math.abs(time2-time1);
		if(abs>1*60*1000){
			String faultTime= DateUtil.getStringDate(record.getFaultTime(), DateUtil.pattern_ymdhms);
			String content="本次故障时间："+faultTime+",该录波文件时间："+startTime;
			return new ValidateContent(content,CfgValidateEnum.FAILED_TIME);
		}
		return new ValidateContent(PARAM_EMPTY,CfgValidateEnum.SUCCESS);
	}
	
	private static String[] parseWaveTime(CfgFile cfgFile){
		String[] result = new String[4];
		String StartSecond = null;
		String StartMicro = null;
		String TriggerSecond = null;
		String TriggerMicro = null;
		CfgTimeInfo[] cti = cfgFile.timeInfos;
		if (cti.length == 2) {
			StartSecond = cti[0].yyyy + "-" + (cti[0].MM < 10 ? "0" + cti[0].MM : cti[0].MM)
						+ "-" + (cti[0].dd < 10 ? "0" + cti[0].dd : cti[0].dd) + " "
						+ (cti[0].hh < 10 ? "0" + cti[0].hh : cti[0].hh) + ":"
						+ (cti[0].mm < 10 ? "0" + cti[0].mm : cti[0].mm) + ":"
						+ (cti[0].ss < 10 ? "0" + cti[0].ss : cti[0].ss);
			StartMicro = cti[0].ssssss + "";
			TriggerSecond = cti[1].yyyy + "-"
						+ (cti[1].MM < 10 ? "0" + cti[1].MM : cti[1].MM) + "-"
						+ (cti[1].dd < 10 ? "0" + cti[1].dd : cti[1].dd) + " "
						+ (cti[1].hh < 10 ? "0" + cti[1].hh : cti[1].hh) + ":"
						+ (cti[1].mm < 10 ? "0" + cti[1].mm : cti[0].mm) + ":"
						+ (cti[1].ss < 10 ? "0" + cti[1].ss : cti[1].ss);
			TriggerMicro = cti[1].ssssss + "";
			result[0] = StartSecond;
			result[1] = StartMicro;
			result[2] = TriggerSecond;
			result[3] = TriggerMicro;
		}
		return result;
	} 
	/**
	 * 线路校验
	 * @param ans
	 * @param equipment
	 * @return
	 */
	private  static boolean lineValidate(CfgAnalogChn[] ans,String equipment){
		for(CfgAnalogChn an:ans){
			if(CimUtils.lineNameMatching(CfgChnsParserUtil.getLineNameOfNw(an.ch_id), equipment)){
				return true;
			}
		}
		return false;
	}
	/**
	 * 母线校验
	 * @param ans
	 * @param equipment
	 * @return
	 */
	private static boolean busValidate(CfgAnalogChn[] ans,String equipment){
		for(CfgAnalogChn an:ans){
			if(CimUtils.lineNameMatching(CfgChnsParserUtil.getLineNameOfNw(an.ch_id), equipment)){
				return true;
			}
		}
		return false;
	}
	/**
	 * 变压器校验
	 * @param ans
	 * @param equipment
	 * @return
	 */
	private static boolean powerValidate(CfgAnalogChn[] ans,String equipment){
		for(CfgAnalogChn an:ans){
			String cimname = CfgChnsParserUtil.getBusNameOfNw(an.ch_id,true);
			String chnname = CfgChnsParserUtil.getBusNameOfNw(equipment,false);
			if(CimUtils.busNameMatching(cimname,chnname)){
				return true;
			}
		}
		return false;
	}
	
	
	public static class ValidateContent{
		private String msg;
		CfgValidateEnum msgType;
		public ValidateContent(String msg, CfgValidateEnum msgType) {
			super();
			this.msg = msg;
			this.msgType = msgType;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public CfgValidateEnum getMsgType() {
			return msgType;
		}
		public void setMsgType(CfgValidateEnum msgType) {
			this.msgType = msgType;
		}
	}

}
